package com.zzg.util.result;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.zzg.base.response.BaseResponse;
import com.zzg.base.response.PageResponse;
import com.zzg.consts.Constants;
import com.zzg.enums.ResponseStatusEnum;


/**
 * 接口返回工具
 * @author zzg
 *
 */
public class ResultUtil {
	/**ModelAndView**/
    public static ModelAndView view(String view){
        return new ModelAndView(view);
    }

    public static ModelAndView view(String view, Map<String,Object> model){
        return new ModelAndView(view,model);
    }

    public static ModelAndView redirect(String view){
        return new ModelAndView("redirect:"+view);
    }

    /**vo**/
    public static BaseResponse vo(int code,String message,Object data){
        return new BaseResponse<>(code,message,data);
    }


    /**error**/
    public static BaseResponse error(int code,String message){
        return vo(code,message,null);
    }

    public static BaseResponse error(ResponseStatusEnum statusEnum){
        return vo(statusEnum.getCode(),statusEnum.getMessage(),null);
    }

    public static BaseResponse error(String message){
        return vo(Constants.DEFAULT_ERROR_CODE,message,null);
    }

    /**success**/
    public static BaseResponse success(String message,Object data){
        return vo(Constants.DEFAULT_SUCCESS_CODE,message,data);
    }

    public static BaseResponse success(String message){
        return success(message,null);
    }

    public static BaseResponse success(ResponseStatusEnum statusEnum){
        return vo(statusEnum.getCode(),statusEnum.getMessage(),null);
    }

    /**PageResoonse 分页对象响应**/
    public static PageResponse tablePage(Long total, List<?> list){
        return new PageResponse(total,list);
    }

    public static PageResponse tablePage(PageInfo pageInfo){
        if (pageInfo == null){
            return new PageResponse(0L,new ArrayList());
        }
        return tablePage(pageInfo.getTotal(),pageInfo.getList());
    }
	
}
